<?php
namespace Admin\Controller;

use Utuo\BackgroundController;
use Utuo\Region;
use Utuo\Phpass;

class PartnerController extends BackgroundController{
	
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('合作商管理');
		$this->setActionTitle(array(
				'index' => '合作商列表',
				'add' => '添加合作商',
				'modify' => '修改合作商',
				'recycle' => '回收站'
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
	}
	
	// 菜单列表
	public function indexAction() {
		$this->getPartnerList(0);
	}
	
	// 回收站
	public function recycleAction() {
		$this->getPartnerList(1);
	}
	
	// 获取合作商列表
	private function getPartnerList($is_deleted) {
		$page = $this->getPage();
		$condition = array(
			'this0.is_deleted' => $is_deleted
		);
		$partnerListSql = M()->table(C('DB_PREFIX')."partner this0")->join('LEFT JOIN '.C('DB_PREFIX').'cn_province this1 on this0.province=this1.code')
							->join('LEFT JOIN '.C('DB_PREFIX').'cn_city this2 on this0.city=this2.code')
							->join('LEFT JOIN '.C('DB_PREFIX').'cn_area this3 on this0.area=this3.code')
							->field("this0.*,this1.name as provinceName,this2.name as cityName,this3.name as areaName")
							->select(false);
		$partnerLists = M()->table($partnerListSql." this0")->where($condition)->limit($this->pageSize)->page($page)->order('id')->select();
		$partnerCount = M()->table($partnerListSql." this0")->where($condition)->count();
		$this->assign('partnerLists', $partnerLists);
		$this->showPage($partnerCount, $this->pageSize);
		$this->display();
	}
	
	// 添加合作商
	public function addAction() {
		$d = D("Partner");
		$this->assign("provinceOptions", $this->getOption(Region::getProvinceList(), '', '==请选择省份=='));
		$this->display();
	}
	
	// 提交添加
	public function addPostAction() {
		$d = D('Partner');
		if ($d->create()) {
			$d->password = Phpass::hash($d->password);
			$dd = clone $d;
			if ($id = $d->add()) {
				false !== M("PartnerAdmin")->add(array(
					'pid' => $id,
					'name' => $dd->name,
					'email' => $dd->email,
					'password' => $dd->password,
					'is_super' => 1,
				)) and $this->ajaxSuccess('添加合作商成功');		
			}
			$this->ajaxError('添加合作商失败');
		} else {
			$this->ajaxError('创建数据失败：'.$d->getError());
		}
	}
	
	// 修改合作商
	public function modifyAction() {
		$id = I('id', 0, 'intval');
		$d = D('Partner');
		$partner = $d->getById($id);
		$partner['isPassedOptions'] = $this->getOption($d->isPassed, $partner['is_passed'], '==请选择审核状态==');
		$partner['provinceOptions'] = $this->getOption(Region::getProvinceList(), $partner['province'], '==请选择省份==');
		$partner['cityOptions'] = $this->getOption(Region::getCityListByProvince($partner['province']), $partner['city'], '==请选择城市==');
		$partner['areaOptions'] = $this->getOption(Region::getAreaListByCity($partner['city']), $partner['area'], '==请选择地区==');
		$this->assign('partner', $partner);
		$this->display();
	}
	
	// 提交修改菜单
	public function modifyPostAction() {
		$d = D('Partner');
		if ($d->create()) {
			if (empty($d->password)) {
				unset($d->password);
			} else{
				$d->password = Phpass::hash($d->password);
			}
			if (false !== $d->save()) {
				$this->ajaxSuccess('修改合作商成功');
			} else {
				$this->ajaxError('修改合作商失败');
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 移除合作商
	public function removePostAction() {
		$this->ajaxRemove();
	}
	
	// 恢复合作商
	public function restorePostAction() {
		$this->ajaxRestore();
	}
	
	// 彻底删除
	public function deletePostAction() {
		$this->ajaxDelete();
	}
	
	// 状态更新
	public function setStatusPostAction() {
		$id = I("id", 0, "intval");
		$status = I("status", 0, "intval");
		$module = M("Partner");
		if ($module->where("id={$id}")->setField("status", 1 - $status)) {
			$this->ajaxSuccess();
		} else {
			$this->ajaxError($module->getError());
		}
	}
	
	// 获取城市
	public function getCityPostAction() {
		echo $this->getOption(Region::getCityListByProvince(I('code')), '', '==请选择城市==');
	}
	
	// 获取地区
	public function getAreaPostAction() {
		echo $this->getOption(Region::getAreaListByCity(I('code')), '', '==请选择地区==');
	}
	
}